/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao.ncc;

import java.util.List;
import model.entity.NhaCungCap;
import model.util.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author luanp_000
 */
public class NhaCungCapDAO {

    private static final String LOAD_ALL_NCC = "FROM NhaCungCap";

    public final static int insert(NhaCungCap ncc) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        int id;
        try {
            session.beginTransaction();
            session.save(ncc);
            session.getTransaction().commit();
            id = ncc.getNccMa();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
        }
        return id;
    }

    public static final void update(NhaCungCap ncc) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.update(ncc);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
        }
    }

    public static final void delete(NhaCungCap ncc) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(ncc);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
        }
    }

    public static final List<NhaCungCap> loadAllNCC() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<NhaCungCap> resultList;
        try {
            session.beginTransaction();
            resultList = session.createQuery(LOAD_ALL_NCC).list();
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
        }
        return resultList;
    }
}
